package gov.pr.celepar.sentinela.client.digester;

import java.io.InputStream;
import org.apache.commons.digester.Digester;

public final class LoadSentinelaXmlDigester {
	private InputStream file;

	public LoadSentinelaXmlDigester(InputStream file) {
		this.file = file;
	}

	public Config getConfig() throws Exception {
		return this.carregar(this.file);
	}

	private Config carregar(InputStream xml) throws Exception {
		Digester digester = new Digester();
		digester.setValidating(false);
		digester.addObjectCreate("Config", Config.class);

		digester.addObjectCreate("Config/Atributos", Atributos.class);
		digester.addObjectCreate("Config/Atributos/Atributo", Atributo.class);
		digester.addSetProperties("Config/Atributos/Atributo", "name", "name");
		digester
				.addSetProperties("Config/Atributos/Atributo", "valor", "valor");

		digester.addSetNext("Config/Atributos", "setAtributos");
		digester.addSetNext("Config/Atributos/Atributo", "addAtributo");

		Config cfg = (Config) digester.parse(xml);
		return cfg;
	}
}
